<?php

namespace app\admin\controller;

use think\Controller;

use app\admin\model\WeiboModel;

class Weibo extends Base
{
    //微博列表
    public function index()
    {
        {
            if (request()->isAjax()) {

                $param = input('param.');

                $limit = $param['pageSize'];
                $offset = ($param['pageNumber'] - 1) * $limit;

                $where = [];
                if (!empty($param['searchText'])) {

                    $where['content'] = ['like', '%' . $param['searchText'] . '%'];
                }
                $user = new WeiboModel();
                $selectResult = $user->getWeiboByWhere($where, $offset, $limit);
                foreach($selectResult as $key=>$vo){

                    $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));

                }

                $return['total'] = $user->getAllWeibo($where);  // 总数据
                $return['rows'] = $selectResult;

                return json($return);
            }

            return $this->fetch();
        }
    }

    //查看微博详情
    public function show()
    {
        $id = input('param.id');

        $weibo = new WeiboModel();
//        $weibo = $weibo->getOneWeibo($id);
//        halt($weibo);
        return view('weibo/show',[
            'weibo'=>$weibo->getOneWeibo($id),
        ]);

    }



     public function weiboDel()
     {
         $id = input('param.id');
//         halt($id);
         $weibo = new WeiboModel();
         $flag = $weibo->delWeibo($id);
         return json(msg($flag['code'], $flag['data'], $flag['msg']));
     }


    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        return [
            '查看详情' => [
                'auth' => 'weibo/show',
                'href' => url('weibo/show', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'

            ],
            '删除' => [
                'auth' => 'weibo/show',
                'href' => "javascript:weiboDel(" .$id .")",
                'btnStyle' => 'danger',
                'icon' => 'fa fa-trash-o'
            ],

        ];
    }

}

